
<template>
  <div class="personalCenter">
    <el-container>
      <el-container>
        <el-aside width="100%">
          <el-col :span="20">
            <div class="demo-basic--circle">
              <div class="img">
                <img src="../assets/UserIM.png" alt="" />
              </div>
              <div class="block">
                <span> 用户名：{{ showUserName }} </span>
              </div>
            </div>
          </el-col>
        </el-aside>
        <el-container>
          <el-main>
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span style="float: left"><b>账号绑定</b></span>
                <el-button-group
                  style="float: right; padding: 3px 0"
                  type="text"
                >
                  <el-button
                    type="primary"
                    icon="el-icon-edit"
                    size="medium"
                    round
                  >
                    修改信息
                  </el-button>
                  <el-button
                    type="primary"
                    icon="el-icon-check"
                    size="medium"
                    round
                  >
                    确认修改
                  </el-button>
                </el-button-group>
              </div>

              <!-- <div v-for="o in 1" :key="o" class="text item"> -->
              <div class="Wallet">
                <span class="WalletDTO">
                  钱包余额：{{ WalletDTO.balance }}
                </span>
                <span>
                  <el-button size="mini" type="success" @click="invest">
                    充值
                  </el-button>
                  <el-button size="mini" @click="openGetMoney">
                    提现
                  </el-button>
                </span>
              </div>

              <hr />
              <!-- <div v-for="o in 1" :key="o" class="text item"> -->
              <div>
                银行卡信息：
                <span class="addCard">
                  <el-button type="success" size="mini" @click="handleEdit">
                    添加银行卡
                  </el-button>
                </span>
              </div>
              <el-table :data="tableData2" style="width: 100%">
                <el-table-column prop="cardName" label="银行" width="180">
                </el-table-column>
                <el-table-column prop="cardNumber" label="卡号" align="center">
                </el-table-column>
                <el-table-column label="操作" align="center">
                  <template slot-scope="scope">
                    <!-- <el-button size="mini" @click="handleEdit(scope.row)">
                      编辑
                    </el-button> -->
                    <el-button
                      size="mini"
                      type="danger"
                      @click="handleDelete(scope.row)"
                    >
                      删除
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
              <!-- <div v-for="o in 1" :key="o" class="text item"> -->

              <!-- </div> -->
            </el-card>
            <el-dialog
              :visible="cardFlag"
              title="添加银行卡"
              class="openDialog"
            >
              <el-form>
                <el-form-item>
                  <el-input
                    v-model="cardDTO.cardName"
                    placeholder="请输入要绑定的银行名称"
                  ></el-input>
                </el-form-item>
                <el-form-item>
                  <el-input
                    v-model="cardDTO.cardNumber"
                    placeholder="请输入要绑定的银行卡号"
                  ></el-input>
                </el-form-item>
              </el-form>

              <div slot="footer" class="dialog-footer">
                <el-button @click="cardFlag = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
              </div>
            </el-dialog>
            <!--充值操作 -->

            <el-dialog
              :visible="openInvestFlag"
              title="充值"
              class="openDialog"
            >
              <el-form>
                <el-form-item>
                  <el-input
                    v-model="investMoney"
                    placeholder="请输入要充值的金额"
                  ></el-input>
                </el-form-item>
                <el-form-item>
                  <el-select v-model="id" placeholder="请选择">
                    <el-option
                      v-for="item in tableData2"
                      :key="item.id"
                      :label="item.cardNumber"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-form>

              <div slot="footer" class="dialog-footer">
                <el-button @click="openInvestFlag = false">取 消</el-button>
                <el-button type="primary" @click="saveInvest">确 定</el-button>
              </div>
            </el-dialog>
            <!--提现操作 -->
            <el-dialog
              :visible="openGetMoneyFlag"
              title="提现"
              class="openDialog"
            >
              <el-form>
                <el-form-item>
                  <el-input
                    v-model="getMoney"
                    placeholder="请输入要提现的金额"
                  ></el-input>
                </el-form-item>
                <el-form-item>
                  <el-select v-model="id" placeholder="请选择">
                    <el-option
                      v-for="item in tableData2"
                      :key="item.id"
                      :label="item.cardNumber"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-form>

              <div slot="footer" class="dialog-footer">
                <el-button @click="openGetMoneyFlag = false">取 消</el-button>
                <el-button type="primary" @click="saveGetmoney"
                  >确 定</el-button
                >
              </div>
            </el-dialog>
          </el-main>
        </el-container>
      </el-container>
    </el-container>
  </div>
</template>
<script>
import Cookies from "js-cookie";

export default {
  data() {
    return {
      cardFlag: false,
      openInvestFlag: false,
      openGetMoneyFlag: false,

      //   text: "",

      //   银行
      tableData2: [],
      investMoney: 0,
      getMoney: 0,
      username: "",
      WalletDTO: {
        balance: "",
        token: "",
      },
      cardDTOALL: [],
      cardDTO: {
        token: "",
        cardNumber: "",
        id: 2,
        cardName: "",
      },

      // cardDTO: {

      //       token:"",
      //       cardNumber : "" ,
      //       id : 0,
      //       cardName: ""
      // },
      id: "",
      ProToken: "",
    };
  },
  created() {
    this.username = Cookies.get("username");
    this.getUserInformation();
    this.getUserWalletInformation();
    this.cardDTO.token = Cookies.get("token");
    this.WalletDTO.token = Cookies.get("token");
    this.ProToken = Cookies.get("token");
  },
  methods: {
    //   钱包操作
    saveInvest() {
      this.ProToken = Cookies.get("token");
      this.$http
        .post("http://127.0.0.1:8001/wallet/recharge", {
          params: {
            token: this.ProToken,
            money: this.investMoney,
            // bankCard:this.cardNumber,
            // id: this.item.id,
            id: this.id,
          },
        })
        .then((res) => {
          if (res.data.code == 1000) {
            this.$message({
              message: "充值成功",
              type: "success",
            });
            this.openInvestFlag = !this.openInvestFlag;

            this.getUserInformation();
            this.getUserWalletInformation();
          }
          if (res.data.code != 1000) {
            this.$message({
              message: res.data.message,
              type: "error",
            });
          }
        });
    },
    invest() {
      // alert("1");
      this.openInvestFlag = !this.openInvestFlag;
    },
    saveGetmoney() {
      this.ProToken = Cookies.get("token");
      this.$http
        .post("http://127.0.0.1:8001/wallet/cashout", {
          params: {
            token: this.ProToken,
            money: this.getMoney,
            cardNumber: this.cardDTO.cardNumber,
            id: this.id,
          },
        })
        .then((res) => {
          if (res.data.code == 1000) {
            this.$message({
              message: "提现成功",
              type: "success",
            });
            this.openGetMoneyFlag = !this.openGetMoneyFlag;

            this.getUserInformation();
            this.getUserWalletInformation();
          }
          if (res.data.code != 1000) {
            this.$message({
              message: res.data.message,
              type: "error",
            });
          }
        });
    },
    openGetMoney() {
      this.openGetMoneyFlag = !this.openGetMoneyFlag;
    },

    handleEdit(row) {
      this.cardFlag = !this.cardFlag;
      this.cardNumber = row;
    },

    // 删除卡号
    handleDelete(row) {
      this.cardDTO = row;
      this.$http
        .post("http://127.0.0.1:8001/card/removeOne", {
          id: this.cardDTO.id,
        })
        .then((res) => {
          if (res.data.code == 1000) {
            this.$message({
              message: "删除成功",
              type: "success",
            });

            setTimeout(() => {
              this.listPage();
            }, 300);
            this.getUserInformation();
            this.getUserWalletInformation();
          }
        });
    },
    setToken() {
      this.OrderDTO.token = Cookies.get("token");
    },
    save() {
      this.cardDTO.token = Cookies.get("token");
      this.$http
        .post("http://127.0.0.1:8001/card/bindcard", {
          params: {
            token: this.cardDTO.token,
            cardName: this.cardDTO.cardName,
            cardNumber: this.cardDTO.cardNumber,
          },
        })
        .then((res) => {
          if (res.data.code == 1000) {
            this.$message({
              message: "绑定成功",
              type: "success",
            });
            this.cardFlag = !this.cardFlag;

            this.getUserInformation();
            this.getUserWalletInformation();
          }
        });
    },
    // 提前拿到用户名查银行卡
    getUserInformation() {
      this.cardDTO.token = Cookies.get("token");
      this.$http
        .post("http://127.0.0.1:8001/card/getCardByUserId", {
          params: { token: this.cardDTO.token },
        })
        .then((res) => {
          console.info(res);

          this.cardDTO.cardNumber = res.data.data.cardNumber;
          this.tableData2 = res.data.data;
        });
    },
    //查询钱包
    getUserWalletInformation() {
      this.WalletDTO.token = Cookies.get("token");
      this.$http
        .post("http://127.0.0.1:8001/wallet/requireWallet", {
          params: { token: this.WalletDTO.token },
        })
        .then((res) => {
          console.info(res);

          // this.WalletDTO.balance= res.data.data.balance;
          this.WalletDTO.balance = res.data.data.balance;
          console.info(this.WalletDTO.balance);
        });
    },
  },
  computed: {
    showUserName() {
      return Cookies.get("username");
    },
  },
};
</script>
<style scoped>
.addCard {
  float: right;
}
.Wallet {
  display: flex;
  flex-direction: column;
}
img {
  border-radius: 50%;
  width: 160px;
}
.personalCenter {
  /* z-index: 0; */
  position: absolute;

  top: 60px;
  width: 100%;
  display: flex;
  flex-direction: column;
}
.el-container {
  display: flex;
  flex-direction: column;
}
.el-header {
  line-height: 60px;
  text-align: center;
  background-color: #b3c0d1;
}
.userInformation {
  display: flex;
  flex-direction: column;
}
.el-input {
  width: 4a00px;
}
.el-aside {
  position: relative;
  display: flex;
  line-height: 44px;
  height: 250px;
  width: 500px;
  text-align: center;
  background-color: #d3dce6;
}
.el-main {
  flex-direction: row-reverse;
  position: relative;
  /* top: 260px; */
  width: 100%;
  line-height: 36px;
  text-align: center;
  background-color: #e9eef3;
}

.demo-basic--circle {
  /* align: 'center'; */
  margin-top: 30px;
  margin-left: 150px;
}
.block {
  font-weight: bold;
}
.text {
  font-size: 14px;
}
.item {
  margin-bottom: 18px;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
  border-radius: 30px;
}
.openDialog {
  margin-left: 25%;
  width: 50%;
}
.el-select {
  width: 100%;
}
</style>

